package com.example.cheeseerpadmin.bus.service.impl;

import com.example.cheeseerpadmin.bus.entity.BusGoods;
import com.example.cheeseerpadmin.bus.entity.BusInport;
import com.example.cheeseerpadmin.bus.mapper.BusGoodsMapper;
import com.example.cheeseerpadmin.bus.mapper.BusInportMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cheeseerpadmin.bus.service.BusGoodsService;
import com.example.cheeseerpadmin.bus.service.BusInportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cheese
 * @since 2022-02-17
 */
@Service
@Transactional
public class BusInportServiceImpl extends ServiceImpl<BusInportMapper, BusInport> implements BusInportService {

    /**
     * 这里，其实我们可以不使用service类，因为使用了service类需要走缓存，在这里，我们一般不需要走缓存
     * 因为牵扯到了敏感数据的变化，走缓存没必要
     */
    @Autowired
    private BusGoodsMapper busGoodsMapper;//这里如果说Bean当中没有代理的话，我们需要去到对应的mapper接口类加上@Mapper注解让它自动注入到Bean管理容器当中

    @Override
    public boolean save(BusInport entity) {
        /**
         * 因为保存一条进货信息，相应的商品当中的库存数量信息也要发生改变。
         * 所以我们需要在进货服务实现类当中重写save方法，当它保存的时候，顺便也将商品数量信息改变一下
         */
        BusGoods busGoods = busGoodsMapper.selectById(entity.getGoodsid());
        busGoods.setNumber(busGoods.getNumber() + entity.getNumber());
        busGoodsMapper.updateById(busGoods);
        return super.save(entity);
    }

    @Override
    public boolean updateById(BusInport entity) {
        /**
         * 修改进货信息的数量时，我们需要减掉原先的进货数量然后再加上修改以后的数量，这就是我们进货信息修改以后对应商品库存数量的值
         */
        BusGoods busGoods = busGoodsMapper.selectById(entity.getGoodsid());
        BusInport busInport = this.getBaseMapper().selectById(entity.getId());
        busGoods.setNumber(busGoods.getNumber() - busInport.getNumber() + entity.getNumber());
        busGoodsMapper.updateById(busGoods);
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        BusInport busInport = this.getBaseMapper().selectById(id);
        BusGoods busGoods = busGoodsMapper.selectById(busInport.getGoodsid());
        busGoods.setNumber(busGoods.getNumber() - busInport.getNumber());
        busGoodsMapper.updateById(busGoods);
        return super.removeById(id);
    }
}
